package Views;

import Controllers.EmployeeCTR;
import Models.Employee;
import Models.Location;
import Views.TableModels.EmployeeTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * @author Thomas
 */
public class EmployeePicker extends javax.swing.JFrame implements ListSelectionListener
{
	private Employee chosenEmployee;
	private EmployeeCTR controller = new EmployeeCTR();

    public EmployeePicker() 
	{
		initComponents();
		employeeTable.getSelectionModel().addListSelectionListener(this);
		employeeTable.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseClicked(MouseEvent e){
				if (e.getClickCount() == 2)
				{
					if(employeeTable.getSelectedRowCount() == 1)
					{
						chooseButtonActionPerformed(null);
					}
				}
			}
		 });
    }

	public Employee getChosenEmployee()
	{
		return this.chosenEmployee;
	}

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        locationBG = new javax.swing.ButtonGroup();
        nameLabel = new javax.swing.JLabel();
        nameTF = new javax.swing.JTextField();
        locationLabel = new javax.swing.JLabel();
        nowhereRB = new javax.swing.JRadioButton();
        lumberYardRB = new javax.swing.JRadioButton();
        hardwareStoreRB = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        employeeTable = new javax.swing.JTable();
        searchbutton = new javax.swing.JButton();
        chooseButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Vælg ansat");

        nameLabel.setText("Navn:");

        locationLabel.setText("Afdeling:");

        locationBG.add(nowhereRB);
        nowhereRB.setSelected(true);
        nowhereRB.setText("-");

        locationBG.add(lumberYardRB);
        lumberYardRB.setText("Trælasthandel");

        locationBG.add(hardwareStoreRB);
        hardwareStoreRB.setText("Byggemarked");

        employeeTable.setModel(new EmployeeTableModel());
        jScrollPane1.setViewportView(employeeTable);

        searchbutton.setText("Søg");
        searchbutton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchbuttonActionPerformed(evt);
            }
        });

        chooseButton.setText("Vælg");
        chooseButton.setEnabled(false);
        chooseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Annuller");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(nameLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(nameTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 140, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                        .add(layout.createSequentialGroup()
                            .add(chooseButton)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(cancelButton))
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 375, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(locationLabel)
                        .add(5, 5, 5)
                        .add(nowhereRB)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(lumberYardRB)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(hardwareStoreRB))
                    .add(searchbutton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(nameLabel)
                    .add(nameTF, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(locationLabel)
                    .add(nowhereRB)
                    .add(lumberYardRB)
                    .add(hardwareStoreRB))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(searchbutton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 93, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cancelButton)
                    .add(chooseButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void searchbuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchbuttonActionPerformed
		Location location = null;
		if(lumberYardRB.isSelected())
		{ location = Location.LumberYard; }
		else if(hardwareStoreRB.isSelected())
		{ location = Location.HardwareStore; }
		this.employeeTable.setModel(new EmployeeTableModel(this.controller.findEmployee(nameTF.getText(), location)));
	}//GEN-LAST:event_searchbuttonActionPerformed

	private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
		this.dispose();
}//GEN-LAST:event_cancelButtonActionPerformed

	private void chooseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseButtonActionPerformed
		int chosenId = Integer.parseInt(employeeTable.getValueAt(employeeTable.getSelectedRow(), 0).toString());
		this.chosenEmployee = this.controller.getEmployee(chosenId);
		this.doClose();
}//GEN-LAST:event_chooseButtonActionPerformed

	public void valueChanged(ListSelectionEvent e)
	{
		if(employeeTable.getSelectedRowCount() == 1)
		{
			chooseButton.setEnabled(true);
		}
		else
		{
			chooseButton.setEnabled(false);
		}
	}

	public void doClose()
	{
		WindowEvent evt = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
		processWindowEvent(evt);
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton chooseButton;
    private javax.swing.JTable employeeTable;
    private javax.swing.JRadioButton hardwareStoreRB;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.ButtonGroup locationBG;
    private javax.swing.JLabel locationLabel;
    private javax.swing.JRadioButton lumberYardRB;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTF;
    private javax.swing.JRadioButton nowhereRB;
    private javax.swing.JButton searchbutton;
    // End of variables declaration//GEN-END:variables

}
